package kr.co.dcinside.agitater.structure;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import kr.co.dcinside.agitater.reader.ArticleReader;

public class Article
{
    private final String writer;
    private final String title;
    static private LinkedList< Article > articles = new LinkedList< Article >();
    private final String time;
    private final String articleLink;
    private int internalIndex = -1;

    private Integer articleNumber = null;
    static private HashMap< String, Article > map = new HashMap< String, Article >();

    private Article( String title, String writer, String time,
        String articleLink )
    {
        this.title = title;
        this.writer = writer;
        this.time = time;
        this.articleLink = articleLink;
    }

    static public void createArticle( String title, String writer, String time,
        String articleLink )
    {
        Article article = new Article( title, writer, time, articleLink );

        if ( map.containsKey( articleLink ) )
        {
            Article oldArticle = map.get( articleLink );
            map.remove( oldArticle );
            articles.remove( oldArticle.internalIndex );
            articles.add( oldArticle.internalIndex, article );
            try
            {
                new ArticleReader()
                {

                    @Override
                    public void refresh()
                    {
                        // TODO Auto-generated method stub

                    }
                }.read( article );
            }
            catch ( IOException e )
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
        else
        {
            map.put( article.getArticleLink(), article );
            article.internalIndex = articles.size();
            articles.add( article );
        }
    }

    static public List< Article > getArticles()
    {
        return articles;
    }

    public String getTitle()
    {
        return title;
    }

    public String getWriter()
    {
        return writer;
    }

    public String getArticleLink()
    {
        return articleLink;
    }

    public String getTime()
    {
        return time;
    }

    static Pattern articleNumberPattern = Pattern.compile( "no=[0-9]*" );

    public int getArticleNumber()
    {
        if ( articleNumber == null )
        {
            Matcher matcher = articleNumberPattern.matcher( articleLink );
            try
            {
                matcher.find();
                String chunk = matcher.group();
                articleNumber = Integer.parseInt( chunk.split( "=" )[1] );
            }
            catch ( Exception e )
            {
                // indicate error
                articleNumber = Integer.MIN_VALUE;
            }
        }
        return articleNumber;
    }
}
